$(function () {
    layui.use(['layer'], function () {
        var layer = layui.layer;
    })
    // 页面初始化生成验证码
    window.onload = createCode('#loginCode');
    // 验证码切换
    $('#loginCode').click(function () {
        createCode('#loginCode');
    });
    // 登陆事件
    $('#loginBtn').click(function () {
        login();
    });
    // 注册事件
    $('#loginRegister').click(function () {
        register();
    });
    // 按回车触发登录
    document.onkeydown = function (e) {
        // 兼容FF和IE和Opera
        let theEvent = window.event || e;
        let code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code == 13) {
            login();
        }
    }

});


// 生成验证码
function createCode(codeID) {
    var code = "";
    // 验证码长度
    var codeLength = 4;
    // 验证码dom元素
    var checkCode = $(codeID);
    // 验证码随机数
    var random = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    for (var i = 0; i < codeLength; i++) {
        // 随机数索引
        var index = Math.floor(Math.random() * 36);
        code += random[index];
    }
    // 将生成的随机验证码赋值
    checkCode.val(code);
}

// 登录 校验验证码、用户名、密码
function validateCode(inputID, codeID) {
    var inputCode = $(inputID).val().toUpperCase();
    var cardCode = $(codeID).val();
    var loginUsername = $('#loginUsername').val();
    var loginPassword = $('#loginPassword').val();
    if ($.trim(loginUsername) == '' || $.trim(loginUsername).length <= 0) {
        layer.alert("账号不能为空", {icon: 5});
        return false;
    }
    var reg = /^[0-9a-zA-Z]/;
    for (i = 0; i < $.trim(loginUsername).length; i++) {
        if (!reg.test($.trim(loginUsername))) {
            layer.alert("账号只能输入字母或数字", {icon: 5});
            return false;
        }
    }
    if ($.trim(loginUsername).length < 4 || $.trim(loginUsername).length > 16) {
        layer.alert("账号只能输入4-16个字符", {icon: 5})
        return false;
    }
    if ($.trim(loginPassword) == '' || $.trim(loginPassword).length <= 0) {
        layer.alert("密码不能为空", {icon: 5});
        return false;
    }
    if ($.trim(loginPassword).length < 6 || $.trim(loginPassword).length > 12) {
        layer.alert("密码必须在 6-12 个字符", {icon: 5});
        return false;
    }
    if (inputCode.length <= 0) {
        layer.alert("验证码不能为空", {icon: 5});
        return false;
    }
    if (inputCode != cardCode) {
        layer.alert("请输入正确验证码", {icon: 5});
        return false;
    }
    return true;
}

// 登录流程
function login() {
    var loginFlg = false;
    if ($('#loginUsername').val() != 'admin') {
        loginFlg = !validateCode('#loginCard', '#loginCode');
    }
    if (loginFlg) {
        //阻断提示
    } else {
        var loginUsername = $('#loginUsername').val();
        var loginPassword = $('#loginPassword').val();
        var params = {};
        params.userAccount = loginUsername;
        params.userPassword = loginPassword;
        var loginLoadIndex = layer.load(2);
        var headers = {}
        $('#loginBtn').val("正在登录...");
        $.DPost("/l/v1/login", params, headers, function (data) {
            if (data.code === '00000') {
                layer.msg("登录成功", {icon: 1, time: 2000}, function () {
                    localStorage.setItem('auth_token', data.data.token);
                    localStorage.setItem('loginUserInfo', JSON.stringify(data.data.user));
                    window.location.href = '/home';
                });
            } else {
                layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                    layer.close(loginLoadIndex);
                    $('#loginBtn').val("登录");
                });
            }
        });
    }

}

// 注册流程
function register() {
    layer.open({
        type: '1',
        content: $('.registerPage'),
        title: '注册',
        area: ['430px', '400px'],
        btn: ['注册', '重置', '取消'],
        closeBtn: '1',
        btn1: function (index, layero) {
            if (!validateRegisterCode()) {

            } else {
                //注册回调
                layer.close(index);
                var registerUsername = $('#registerUsername').val();
                var registerPassword = $('#registerPassword').val();
                var registerWellPassword = $('#registerWellPassword').val();
                var selectValue = $('#roleSelect option:selected').val();
                var params = {};
                params.registerUsername = registerUsername;
                params.registerPassword = registerPassword;
                params.useRepetitionPassword = registerWellPassword;
                params.selectValue = selectValue; // 2-司机 3-乘客
                var headers = {}
                var registerLoadIndex = layer.load(2);
                $.DPost("/l/v1/register", params, headers, function (data) {
                    if (data.code === '00000') {
                        layer.msg("注册成功", {icon: 1, time: 2000}, function () {
                            layer.close(registerLoadIndex);
                            window.location.href = '/login';
                        });
                    } else {
                        layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                            layer.close(registerLoadIndex);
                        });
                    }
                });
            }
        },
        btn2: function (index, layero) {
            //重置回调
            var registerUsername = $('#registerUsername').val("");
            var registerPassword = $('#registerPassword').val("");
            var registerWellPassword = $('#registerWellPassword').val("");
            // 防止注册页面关闭
            return false;
        },
        btn3: function (index, layero) {
            //取消回调
        }
    })
}

// 注册 校验验证码、用户名、密码
function validateRegisterCode() {
    var registerUsername = $('#registerUsername').val();
    var registerPassword = $('#registerPassword').val();
    var registerWellPassword = $('#registerWellPassword').val();
    if ($.trim(registerUsername) == '' || $.trim(registerUsername).length <= 0) {
        layer.alert("用户名不能为空", {icon: 5});
        return false;
    }
    var reg = /^[0-9a-zA-Z]/;
    for (i = 0; i < $.trim(registerUsername).length; i++) {
        if (!reg.test($.trim(registerUsername))) {
            layer.alert("账号只能输入字母或数字", {icon: 5});
            return false;
        }
    }
    if ($.trim(registerUsername).length < 4 || $.trim(registerUsername).length > 16) {
        layer.alert("账号只能输入4-16个字符", {icon: 5})
        return false;
    }
    if ($.trim(registerPassword) == '' || $.trim(registerPassword).length <= 0) {
        layer.alert("密码不能为空", {icon: 5});
        return false;
    }
    if ($.trim(registerPassword).length < 6 || $.trim(registerPassword).length > 12) {
        layer.alert("密码必须在 6-12 个字符。", {icon: 5});
        return false;
    }
    if ($.trim(registerWellPassword) == '' || $.trim(registerWellPassword).length <= 0) {
        layer.alert("重复密码不能为空", {icon: 5});
        return false;
    }
    if ($.trim(registerPassword) != $.trim(registerWellPassword)) {
        layer.alert("两次密码不一致。", {icon: 5});
        return false;
    }
    return true;
}
